package com.itany.corejava.code12_线程;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年07月30日15:04
 */
public class Test08_同步代码块 {
    public static void main(String[] args) {
        OneThread t1=new OneThread();
        OneThread t2=new OneThread();
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
}
class OneThread extends  Thread{
    static int num=100;
    static Object object=new Object();

    @Override
    public void run() {
        while(true){
            //同步代码块:被synchronized包围的代码块
            //锁对象，要保证锁对象是相同的
            synchronized (object){
                if(num==0){
                    break;
                }else{
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"正在出票:"+num--);
                }
            }
        }
    }
}